package io.github.idol.roomlist

import android.app.Activity
import android.arch.lifecycle.Observer
import android.content.Intent
import android.databinding.DataBindingUtil
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.RecyclerView
import android.util.Log
import io.github.idol.R
import io.github.idol.data.RoomListInfo
import io.github.idol.databinding.ActivityRoomListBinding
import io.github.idol.roomlist.room.RoomDetailActivity
import io.github.idol.roomlist.room.RoomDetailAdapter
import io.github.idol.utils.InjectorUtils

class RoomListActivity : AppCompatActivity() {

    private lateinit var binding : ActivityRoomListBinding
    private lateinit var viewModel: RoomListViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_room_list)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_room_list)
        viewModel = InjectorUtils.provideLoginViewModelFactory(application).create(RoomListViewModel::class.java)
        val adapter = RoomListAdapter(object :RoomListAdapter.Callback{
            override fun onRoomClick(content: RoomListInfo.ContentBean) {
                Log.i("TAG","onRoomClick")
                RoomDetailActivity.startRoomListActivity(this@RoomListActivity,content.roomId)
            }
        })
        binding.roomList.adapter = adapter
        binding.roomList.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL))

        viewModel.getRoomListData().observe(this, Observer {
            adapter.submitList(it)
        })

        viewModel.start()
    }

    companion object {
        fun startRoomListActivity(activity: Activity){
            val intent = Intent(activity, RoomListActivity::class.java)
            activity.startActivity(intent)
        }
    }
}
